package com.bw.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class UserServiceSecurity implements UserDetailsService {


    @Autowired
    PasswordEncoder encoder;

    @Autowired
    com.bw.service.UserService userService;
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        com.bw.entity.User user = new  com.bw.entity.User();
        user.setLoginName(s);
        List<com.bw.entity.User> users = userService.selectByCondition(user);
        System.out.println(encoder.encode("123456"));

        if (users==null || users.size() == 0){
            throw  new UsernameNotFoundException("用户名不存在");
        }
        return new User(s,users.get(0).getPassword(),true,true,true,true, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
